package com.castle.fortress.admin.study.exam.rest;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.dto.MemberDto;
import com.castle.fortress.admin.member.service.MemberAuthorityService;
import com.castle.fortress.admin.study.exam.dto.*;
import com.castle.fortress.admin.study.exam.entity.*;
import com.castle.fortress.admin.study.exam.service.*;
import com.castle.fortress.common.enums.AuthorityTypeEnum;
import com.castle.fortress.common.enums.YesNoEnum;
import com.castle.fortress.common.respcode.BizErrorCode;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 试卷管理 api 控制器
 *
 * @author castle
 * @since 2023-02-10
 */
@Api(tags = "试卷管理api管理控制器")
@RestController
@RequestMapping("/api/study/exam/studyExam")
public class ApiStudyExamController {
    @Autowired
    private StudyExamService studyExamService;
    @Autowired
    private MemberAuthorityService memberAuthorityService;
    @Autowired
    private StudyExamStatisticsMemberService studyExamStatisticsMemberService;
    @Autowired
    private StudyMemberUnfinishExamService studyMemberUnfinishExamService;
    @Autowired
    private StudyExamQuestionService studyExamQuestionService;
    @Autowired
    private StudyMemberExamService studyMemberExamService;


    /**
     * 试卷管理的分页展示 获取用户可查看的
     *
     * @param studyExamDto 试卷管理实体类
     * @param currentPage  当前页
     * @param size         每页记录数
     * @return
     */
    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.QUERY)
    @ApiOperation("试卷管理分页展示")
    @GetMapping("/page")
    public RespBody<IPage<StudyExamDto>> pageStudyExam(StudyExamDto studyExamDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false) Integer size) {
        Integer pageIndex = currentPage == null ? GlobalConstants.DEFAULT_PAGE_INDEX : currentPage;
        Integer pageSize = size == null ? GlobalConstants.DEFAULT_PAGE_SIZE : size;
        Page<StudyExamDto> page = new Page(pageIndex, pageSize);

        IPage<StudyExamDto> pages = studyExamService.pageStudyExamExtendsApi(page, studyExamDto);
        return RespBody.data(pages);
    }

    /**
     * 试卷管理保存
     *
     * @param studyExamDto 试卷管理实体类
     * @return
     */
    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.INSERT)
    @ApiOperation("试卷管理保存")
    @PostMapping("/save")
    public RespBody<String> saveStudyExam(@RequestBody StudyExamDto studyExamDto) {
        if (studyExamDto == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyExamEntity studyExamEntity = ConvertUtil.transformObj(studyExamDto, StudyExamEntity.class);
        if (studyExamService.save(studyExamEntity)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 试卷管理编辑
     *
     * @param studyExamDto 试卷管理实体类
     * @return
     */
    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.UPDATE)
    @ApiOperation("试卷管理编辑")
    @PostMapping("/edit")
    public RespBody<String> updateStudyExam(@RequestBody StudyExamDto studyExamDto) {
        if (studyExamDto == null || studyExamDto.getId() == null || studyExamDto.getId().equals(0L)) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyExamEntity studyExamEntity = ConvertUtil.transformObj(studyExamDto, StudyExamEntity.class);
        if (studyExamService.updateById(studyExamEntity)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 试卷管理删除
     *
     * @param ids 试卷管理id集合
     * @return
     */
    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.DELETE)
    @ApiOperation("试卷管理删除")
    @PostMapping("/delete")
    public RespBody<String> deleteStudyExam(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if (studyExamService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 试卷管理详情
     *
     * @param id 试卷管理id
     * @return
     */
    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.QUERY)
    @ApiOperation("试卷管理详情")
    @GetMapping("/info")
    public RespBody<StudyExamDto> infoStudyExam(@RequestParam Long id) {
        if (id == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyExamEntity studyExamEntity = studyExamService.getById(id);
        StudyExamDto studyExamDto = ConvertUtil.transformObj(studyExamEntity, StudyExamDto.class);
        return RespBody.data(studyExamDto);
    }

    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.QUERY)
    @ApiOperation("未答的试卷列表")
    @GetMapping("/page/unanswered")
    public RespBody<IPage<StudyExamDto>> unanswered(@RequestParam(required = false) Integer currentPage, @RequestParam(required = false) Integer size) {
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        Integer pageIndex = currentPage == null ? GlobalConstants.DEFAULT_PAGE_INDEX : currentPage;
        Integer pageSize = size == null ? GlobalConstants.DEFAULT_PAGE_SIZE : size;
        Page<StudyExamDto> page = new Page(pageIndex, pageSize);
        IPage<StudyExamDto> pages = studyExamService.unanswered(page, member.getId());
        return RespBody.data(pages);
    }

    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.QUERY)
    @ApiOperation("待提醒的试卷列表")
    @GetMapping("/list/toRemind")
    public RespBody<List<StudyExamDto>> toRemind() {
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        List<StudyExamDto> list = studyExamService.toRemindList(member.getId());
        return RespBody.data(list);
    }

    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.QUERY)
    @ApiOperation("已答的试卷列表")
    @GetMapping("/page/answeredExams")
    public RespBody<IPage<StudyExamDto>> answeredExams(@RequestParam(required = false) Integer currentPage, @RequestParam(required = false) Integer size) {
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        Integer pageIndex = currentPage == null ? GlobalConstants.DEFAULT_PAGE_INDEX : currentPage;
        Integer pageSize = size == null ? GlobalConstants.DEFAULT_PAGE_SIZE : size;
        Page<StudyExamDto> page = new Page(pageIndex, pageSize);
        IPage<StudyExamDto> pages = studyExamService.answeredExams(page, member.getId());
        return RespBody.data(pages);
    }
    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.QUERY)
    @ApiOperation("已结束试卷列表")
    @GetMapping("/page/endTheExams")
    public RespBody<IPage<StudyExamDto>> endTheExams(@RequestParam(required = false) Integer currentPage, @RequestParam(required = false) Integer size) {
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        Integer pageIndex = currentPage == null ? GlobalConstants.DEFAULT_PAGE_INDEX : currentPage;
        Integer pageSize = size == null ? GlobalConstants.DEFAULT_PAGE_SIZE : size;
        Page<StudyExamDto> page = new Page(pageIndex, pageSize);
        IPage<StudyExamDto> pages = studyExamService.endTheExams(page, member.getId());
        return RespBody.data(pages);
    }
    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.QUERY)
    @ApiOperation("试卷信息")
    @GetMapping("/info/{id}")
    public RespBody<StudyExamDto> info(@PathVariable("id") Long id) {
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        if (id == null || id.equals(0)) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if (!memberAuthorityService.checkUserAuthByPoseId(AuthorityTypeEnum.EXAM.getCode(), id, member.getId())) {
            throw new BizException(GlobalRespCode.UNAUTHORIZED);
        }
        StudyExamEntity entity = studyExamService.getById(id);
        if (YesNoEnum.YES.getCode().equals(entity.getIsLimited())) {
            Map<String, Object> map = new HashMap<>();
            map.put("memberId", member.getId());
            map.put("examId", id);
            List<StudyExamStatisticsMemberEntity> list = studyExamStatisticsMemberService.listByMap(map);
            if (list != null && list.size() >= entity.getExamNum()) {
                throw new BizException(BizErrorCode.EXAM_NO_TIMES);
            }
        }
        return RespBody.data(ConvertUtil.transformObj(entity, StudyExamDto.class));
    }

    /**
     * 校验用户是否有未完成的考试
     *
     * @param id
     * @return true 有未完成试卷 false 无未完成试卷
     */
    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.QUERY)
    @ApiOperation("是否有未完成试卷信息")
    @GetMapping("/unfinish/{id}")
    public RespBody<Boolean> unfinishInfo(@PathVariable("id") Long id) {
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        if (id == null || id.equals(0)) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        Map<String, Object> params = new HashMap<>();
        params.put("examId", id);
        params.put("memberId", member.getId());
        QueryWrapper<StudyMemberUnfinishExamEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("exam_id", id);
        wrapper.eq("member_id", member.getId());
        wrapper.orderByAsc("sort");
        List<StudyMemberUnfinishExamEntity> entities = studyMemberUnfinishExamService.list(wrapper);
        if (entities == null || entities.isEmpty()) {
            return RespBody.data(false);
        } else {
            //校验是否已超过试卷时间
            StudyMemberUnfinishExamEntity entity = entities.get(0);
            Date endTime = DateUtil.offsetMinute(entity.getCreateTime(), entity.getExamTime());
            if (new Date().compareTo(endTime) < 0) {
                return RespBody.data(true);
            } else {
                studyMemberUnfinishExamService.deleteExam(id, member.getId());
                return RespBody.data(false);
            }
        }
    }

    /**
     * 校验用户是否有未完成的考试
     *
     * @param id
     * @return true 有未完成试卷 false 无未完成试卷
     */
    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.QUERY)
    @ApiOperation("是否有未完成试卷信息")
    @GetMapping("/unfinish/del/{id}")
    public RespBody<String> unfinishDel(@PathVariable("id") Long id) {
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        if (id == null || id.equals(0)) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        studyMemberUnfinishExamService.deleteExam(id, member.getId());
        return RespBody.data("保存成功");
    }

    @CastleLog(operLocation = "获取试卷基本信息-api", operType = OperationTypeEnum.QUERY)
    @ApiOperation("试卷详情")
    @GetMapping("/detail/basicInfo/{id}")
    public RespBody<StudyExamDto> basicInfo(@PathVariable("id") Long id) {
        Date startDate = new Date();
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        if (id == null || id.equals(0)) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyExamDto dto= studyExamService.getByIdExamNum(id);
        return RespBody.data(dto);
    }

    @CastleLog(operLocation = "获取试卷详情并创建新试卷管理-api", operType = OperationTypeEnum.QUERY)
    @ApiOperation("获取试卷详情并创建新试卷")
    @GetMapping("/detail/{id}")
    public RespBody<StudyExamDto> detail(@PathVariable("id") Long id) {
        Date startDate = new Date();
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        if (id == null || id.equals(0)) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyExamEntity entity = studyExamService.getById(id);
        StudyExamDto dto = ConvertUtil.transformObj(entity, StudyExamDto.class);
        QueryWrapper<StudyExamQuestionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("exam_id", id);
        List<StudyExamQuestionDto> examQuestionDTOS = ConvertUtil.transformObjList(studyExamQuestionService.list(wrapper), StudyExamQuestionDto.class);
        //单选列表
        List<StudyExamQuestionDto> singleList = new ArrayList<>();
        //多选列表
        List<StudyExamQuestionDto> multipleList = new ArrayList<>();
        for (StudyExamQuestionDto tempQuestion : examQuestionDTOS) {
            if ("单选题".equals(tempQuestion.getQuestionType())) {
                tempQuestion.setScore(dto.getSingleScore());
                singleList.add(tempQuestion);
            } else if ("多选题".equals(tempQuestion.getQuestionType())) {
                tempQuestion.setScore(dto.getMultipleScore());
                multipleList.add(tempQuestion);
            }
        }
        //是否乱序
        if (YesNoEnum.YES.getCode().equals(dto.getIsOutOrder())) {
            examQuestionDTOS.clear();
            Collections.shuffle(singleList);
            Collections.shuffle(multipleList);
            examQuestionDTOS.addAll(singleList);
            examQuestionDTOS.addAll(multipleList);
        }
        for (int i = 0; i < examQuestionDTOS.size(); i++) {
            examQuestionDTOS.get(i).setSort(i + 1);
        }
        dto.setExamQuestionDTOList(examQuestionDTOS);
        //异步保存考试信息
        List<StudyMemberUnfinishExamEntity> unfinishExamDTOS = new ArrayList<>();
        for (StudyExamQuestionDto questionDTO : examQuestionDTOS) {
            StudyMemberUnfinishExamEntity unfinishExamDTO = new StudyMemberUnfinishExamEntity();
            BeanUtils.copyProperties(questionDTO, unfinishExamDTO);
            unfinishExamDTO.setExamId(dto.getId());
            unfinishExamDTO.setMemberId(member.getId());
            unfinishExamDTO.setExamTime(dto.getExamTime());
            unfinishExamDTO.setCreateTime(startDate);
            unfinishExamDTO.setExamQuestionId(questionDTO.getId());
            unfinishExamDTO.setScore(questionDTO.getScore());
            unfinishExamDTO.setSort(questionDTO.getSort());
            unfinishExamDTOS.add(unfinishExamDTO);
        }
        // 要创建新的未完成的考试记录  将之前的删除
        studyMemberUnfinishExamService.deleteExam(dto.getId(), member.getId());
        studyMemberUnfinishExamService.saveBatch(unfinishExamDTOS);
        return RespBody.data(dto);
    }

    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.INSERT)
    @ApiOperation("提交会员未完成试卷内容")
    @PostMapping("/saveMemberUnfinishExam")
    public RespBody<Boolean> saveMemberUnfinishExam(@RequestBody StudyMemberUnfinishExamDto examDTO) {
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        if (examDTO == null || examDTO.getExamId() == null || examDTO.getExamId() == 0L) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        examDTO.setMemberId(member.getId());
        //是否还有考试机会
        Boolean haveChance = studyMemberUnfinishExamService.commitUnfinishExam(examDTO);
        return RespBody.data(haveChance);
    }

    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.INSERT)
    @ApiOperation("保存会员试卷内容")
    @PostMapping("/saveMemberExam")
    public RespBody<StudyExamStatisticsMemberDto> saveMemberExam(@RequestBody StudyExamStatisticsMemberDto memberDTO) {
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        if (memberDTO == null || memberDTO.getExamId() == null || memberDTO.getExamId() == 0L) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        memberDTO.setMemberId(member.getId());
        StudyExamDto studyExamDto = studyExamService.getByIdExtends(memberDTO.getExamId());
        memberDTO.setQualifiedScore(studyExamDto.getQualifiedScore()+"");
        memberDTO.setTotalScore(studyExamDto.getTotalScore()+"");
        StudyExamStatisticsMemberDto statisticsMemberDTO = studyExamStatisticsMemberService.saveMemberExam(memberDTO);
        return RespBody.data(statisticsMemberDTO);
    }

    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.QUERY)
    @ApiOperation("用户考试记录详情")
    @GetMapping("/memberExamInfo/{id}")
    public RespBody<StudyExamStatisticsMemberDto> memberExamInfo(@PathVariable("id") Long id) {
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        if (id == null || id.equals(0)) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyExamStatisticsMemberDto statisticsMemberDTO = studyExamStatisticsMemberService.queryStatisticeMemberInfo(id);
        QueryWrapper<StudyMemberExamEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("record_id", id);
        List<StudyMemberExamDto> examDTOS = ConvertUtil.transformObjList(studyMemberExamService.list(wrapper), StudyMemberExamDto.class);
        statisticsMemberDTO.setExamQuestionDTOList(examDTOS);
        return RespBody.data(statisticsMemberDTO);
    }

    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.INSERT)
    @ApiOperation("提交一题")
    @PostMapping("/saveOneQuestion")
    public RespBody<String> saveOneQuestion(@RequestBody StudyMemberUnfinishExamDto examDTO) {
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        if (examDTO == null || examDTO.getExamId() == null || examDTO.getExamId() == 0L || examDTO.getId() == null || examDTO.getId() == 0L) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        examDTO.setMemberId(member.getId());
        examDTO.setExamQuestionId(examDTO.getId());
        if (StrUtil.isEmpty(examDTO.getChoiceAnswer())) {
            throw new BizException(BizErrorCode.QUESTION_NO_ANSWER);
        }
        studyMemberUnfinishExamService.updateOneQuestion(examDTO);
        return RespBody.data("已提交");
    }

    @CastleLog(operLocation = "试卷管理-api", operType = OperationTypeEnum.QUERY)
    @ApiOperation("未完成试卷详情")
    @GetMapping("/unfinishDetail/{id}")
    public RespBody<Map<String, Object>> unfinishDetail(@PathVariable("id") Long id) {
        MemberDto member = WebUtil.currentMember();
        if (member == null) {
            throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
        }
        Map<String, Object> map = new HashMap<>();
        if (id == null || id.equals(0)) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        QueryWrapper<StudyMemberUnfinishExamEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("exam_id", id);
        wrapper.eq("member_id", member.getId());
        wrapper.orderByAsc("sort");
        List<StudyMemberUnfinishExamEntity> examDTOS = studyMemberUnfinishExamService.list(wrapper);

        StudyExamEntity entity = studyExamService.getById(id);
        StudyExamDto dto = ConvertUtil.transformObj(entity, StudyExamDto.class);

        List<StudyExamQuestionDto> examQuestionDTOS = new ArrayList<>();
        Integer examCurrentIndex = 1;
        List<Map<String, Object>> optionsList = null;
        Map<String, Object> tempMap = null;
        for (StudyMemberUnfinishExamEntity unfinishExamDTO : examDTOS) {
            optionsList = new ArrayList<>();
            if (StrUtil.isNotEmpty(unfinishExamDTO.getChoiceAnswer())) {
                examCurrentIndex = unfinishExamDTO.getSort();
            }
            StudyExamQuestionDto questionDTO = new StudyExamQuestionDto();
            BeanUtils.copyProperties(unfinishExamDTO, questionDTO);
            questionDTO.setId(unfinishExamDTO.getExamQuestionId());
            questionDTO.setScore(unfinishExamDTO.getScore());
            questionDTO.setChoiceAnswer(unfinishExamDTO.getChoiceAnswer());
            if (StrUtil.isNotEmpty(unfinishExamDTO.getAnswerA())) {
                tempMap = new HashMap<>();
                tempMap.put("options", "A");
                tempMap.put("text", unfinishExamDTO.getAnswerA());
                tempMap.put("check", StrUtil.isNotEmpty(unfinishExamDTO.getChoiceAnswer()) && unfinishExamDTO.getChoiceAnswer().indexOf("A") != -1 ? true : false);
                optionsList.add(tempMap);
            }
            if (StrUtil.isNotEmpty(unfinishExamDTO.getAnswerB())) {
                tempMap = new HashMap<>();
                tempMap.put("options", "B");
                tempMap.put("text", unfinishExamDTO.getAnswerB());
                tempMap.put("check", StrUtil.isNotEmpty(unfinishExamDTO.getChoiceAnswer()) && unfinishExamDTO.getChoiceAnswer().indexOf("B") != -1 ? true : false);
                optionsList.add(tempMap);
            }
            if (StrUtil.isNotEmpty(unfinishExamDTO.getAnswerC())) {
                tempMap = new HashMap<>();
                tempMap.put("options", "C");
                tempMap.put("text", unfinishExamDTO.getAnswerC());
                tempMap.put("check", StrUtil.isNotEmpty(unfinishExamDTO.getChoiceAnswer()) && unfinishExamDTO.getChoiceAnswer().indexOf("C") != -1 ? true : false);
                optionsList.add(tempMap);
            }
            if (StrUtil.isNotEmpty(unfinishExamDTO.getAnswerD())) {
                tempMap = new HashMap<>();
                tempMap.put("options", "D");
                tempMap.put("text", unfinishExamDTO.getAnswerD());
                tempMap.put("check", StrUtil.isNotEmpty(unfinishExamDTO.getChoiceAnswer()) && unfinishExamDTO.getChoiceAnswer().indexOf("D") != -1 ? true : false);
                optionsList.add(tempMap);
            }
            if (StrUtil.isNotEmpty(unfinishExamDTO.getAnswerE())) {
                tempMap = new HashMap<>();
                tempMap.put("options", "E");
                tempMap.put("text", unfinishExamDTO.getAnswerE());
                tempMap.put("check", StrUtil.isNotEmpty(unfinishExamDTO.getChoiceAnswer()) && unfinishExamDTO.getChoiceAnswer().indexOf("E") != -1 ? true : false);
                optionsList.add(tempMap);
            }
            if (StrUtil.isNotEmpty(unfinishExamDTO.getAnswerF())) {
                tempMap = new HashMap<>();
                tempMap.put("options", "F");
                tempMap.put("text", unfinishExamDTO.getAnswerF());
                tempMap.put("check", StrUtil.isNotEmpty(unfinishExamDTO.getChoiceAnswer()) && unfinishExamDTO.getChoiceAnswer().indexOf("F") != -1 ? true : false);
                optionsList.add(tempMap);
            }
            questionDTO.setOptionsList(optionsList);
            examQuestionDTOS.add(questionDTO);
        }
        StudyMemberUnfinishExamEntity unfinishExamDTO = examDTOS.get(0);
        Integer examTime = unfinishExamDTO.getExamTime();
        long usedTime = DateUtil.between(unfinishExamDTO.getCreateTime(), new Date(), DateUnit.SECOND);
        Long remaining = examTime * 60 - usedTime;
        dto.setExamQuestionDTOList(examQuestionDTOS);
        map.put("examInfo", dto);
        //处理边界值
        if (examCurrentIndex > examQuestionDTOS.size()) {
            examCurrentIndex = examQuestionDTOS.size();
        }
        map.put("examCurrentIndex", examCurrentIndex);
        map.put("remaining", remaining);
        return RespBody.data(map);
    }

}
